//
//  LCShengJingTextViewController.m
//  JKFuYinProject
//
//  Created by admin on 2018/1/8.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LCShengJingTextViewController.h"
#import "LCShengJingTextViewController+LoadData.h"
#import "LCShengJingTextViewController+LCAVPlay.h"
#import "LCShengJingTextViewController+LCSettingUI.h"
#import "LCShengJingTextViewController+LCSetupHeaderView.h"
#import "TheHomeManager.h"
#import "TheHomeManager+Additions.h"
#import "LCHistoryDataManager.h"

@interface LCShengJingTextViewController ()

@end

@implementation LCShengJingTextViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.managerShengJingView.hidden = NO;
    [LCFloatingWindow sharedInstance].hidden = NO;//安装AVPlay
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    self.managerShengJingView.hidden = YES;
#pragma mark -  重置阅读字体大小  -
    [[NSUserDefaults standardUserDefaults] setFloat:19 forKey:@"fontSizeTemp"];
    self.fuzhiStr = @"";
    NSLog(@"圣经章页已销毁");
    [LCFloatingWindow sharedInstance].hidden = YES;//安装AVPlay
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tabBarController.tabBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    [LCFloatingWindow sharedInstance].isOpenHiddenPanDuan = YES;
    self.fontSizeTemp = 0.0;
    
    [self setupNV];///top
    [self setupContentTextUI];///圣经文本内容
    [self setupHeaderView];
    [self loadTopAndHeaderData];
    [self setupHeaderSettingBtn];///设置右侧按钮
    [self setupSettingUI];///设置右侧按钮
    
    [self refreshHistoryPlay];
    [self creareAVPlay];//安装AVPlay
}

-(void)refreshHistoryPlay{
    [self refreshData];
    [self.bibleTableViewController.tableView reloadData];
    
    if (_LCPlayer.playingShengJing == 1/**判断正在播放圣经或者已经播放过暂停中*/) {
        /**让歌词恢复成原来的位置,其他无作用*/
        [self.bibleTableViewController refreshUIWithCurrentTime:_LCPlayer.currentTime totalTime:_LCPlayer.totalTime];
        return;
    }
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSString *titleStr = [LCHistoryDataManager loadBibleTextTitleData];
        //给进入圣经页加载播放列表
        [[TheHomeManager shareTheHomeManager] localShengJingSongMessage:[_TheHomeM getSongParentIdWithTitle:titleStr]];
        LCPlaylist *playlistTest = _PlaylistMgr.allPlaylists.firstObject;
        _LCPlayer.songs = playlistTest.allSongs;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            /**没有播放圣经*/
            _LCPlayer.avPlayer.volume = 0;
            if (_LCPlayer.songs != nil) {
                [_LCPlayer playAtIndex:[[titleStr substringWithinBoundsLeft:@"第" right:@"章"] integerValue] -1 isSelectPlay:YES];
            }
            [_LCPlayer pause];
            _LCPlayer.avPlayer.volume = 1;
        });
    });
}

#pragma mark -  ///设置右侧按钮 和圣经文本内容  -
///设置右侧按钮 和圣经文本内容
- (void)setupContentTextUI{
    self.automaticallyAdjustsScrollViewInsets = NO;
    LCBibleTableViewController *bibleTableViewController = [[LCBibleTableViewController alloc] initWithStyle:UITableViewStylePlain];
    _bibleTableViewController = bibleTableViewController;
    __weak typeof(self) weakSelf = self;
    @weakify(self)
    bibleTableViewController.pushShowComposeVCBlock = ^(NSString *jieStr, NSString *contentStr) {
        @strongify(self)
        if ([[_UserM usercode] isEqualToString:@""]) {
            pushAccLoginVC
            return;
        }
        LCShowComposeController *viewController = [[LCShowComposeController alloc] init];
        viewController.titleStr = _LCPlayer.playingSong.title;
        viewController.jieStr = jieStr;
        viewController.contentStr = contentStr;
        [weakSelf.navigationController pushViewController:viewController animated:YES];
        [weakSelf animationHiddenManagerShengJingView];
    };
    
    bibleTableViewController.collectionBlock = ^{
        [weakSelf didSelectRowAtIndexPath];
    };
    
    [self.view addSubview:bibleTableViewController.tableView];
    [bibleTableViewController.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        if (@available(iOS 11, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-60);
        }else{
            make.top.equalTo(self.view.mas_top);
            make.bottom.offset(-60);
        }
    }]; //添加歌词tableView
}



///显示圣经分享面板
- (void)animationShowManagerShengJingView {
    [UIView animateWithDuration:0.5 animations:^{
        self.managerShengJingView.alpha = 1;
        if (iPhoneX) {
            self.managerShengJingView.frame = CGRectMake(0, self.view.height-6, self.view.width, 60);
        }else{
            self.managerShengJingView.frame = CGRectMake(0, self.view.height, self.view.width, 60);
        }
    } completion:^(BOOL finished) {
    }];
}

///隐藏圣经分享面板
- (void)animationHiddenManagerShengJingView {
    [self.bibleTableViewController.indexPathArr removeAllObjects];
    [self.bibleTableViewController.selectedData removeAllObjects];
    [self.bibleTableViewController.tableView reloadData];
    
    [UIView animateWithDuration:0.5 animations:^{
        self.managerShengJingView.alpha = 0;
        self.managerShengJingView.frame = CGRectMake(0, self.view.height+60+HOME_INDICATOR_HEIGHT, self.view.width, 60);
    } completion:^(BOOL finished) {
    }];
}

- (void)didSelectRowAtIndexPath{
    [[[UIApplication sharedApplication].delegate window] bringSubviewToFront:_managerShengJingView];
    [self.view bringSubviewToFront:self.managerShengJingView];
    
    [self animationShowManagerShengJingView];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.bibleTableViewController.selectedData.count == 0) {
            [self animationHiddenManagerShengJingView];
        }
    });
    
    NSLog(@"点击触发点击触发点击触发点击触发点击触发");
}


- (void)dealloc {
    [LCFloatingWindow sharedInstance].isOpenHiddenPanDuan = NO;
    [self.managerShengJingView removeFromSuperview];
    [self.view removeAllSubviews];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

